package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constant.SystemConstants;
import com.sangeng.constant.enums.AppHttpCodeEnum;
import com.sangeng.domain.core.ResponseResult;
import com.sangeng.domain.dto.CategoryPageDto;
import com.sangeng.domain.entity.Article;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.BlogCategoryVo;
import com.sangeng.domain.dto.CategoryAddDto;
import com.sangeng.domain.vo.CategoryVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.CategoryMapper;
import com.sangeng.service.ArticleService;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2025-06-13 15:08:49
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        // 查询文章表，状态为已发布的文章
        List<Article> ids = articleService.list(
                new LambdaQueryWrapper<Article>()
                        .eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL)
        );
        // 获取文章的分类id，并且去重
        Set<Long> categoryIds = ids.stream().map(Article::getCategoryId).collect(Collectors.toSet());

        // 查询分类表
        List<Category> categories = listByIds(categoryIds);

        List<Category> filterCategories = categories.stream().filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus())).toList();
        // 变为Vo对象
        List<CategoryVo> categoryVos = BeanUtil.copyBeanList(filterCategories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult listAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus, SystemConstants.STATUS_NORMAL);
        List<BlogCategoryVo> blogCategoryVos = BeanUtil.copyBeanList(list(queryWrapper), BlogCategoryVo.class);
        return ResponseResult.okResult(blogCategoryVos);
    }

    @Override
    public ResponseResult listAllCategoryPage(CategoryPageDto categoryPageDto) {
        // 构建查询条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(!Objects.isNull(categoryPageDto.getName()), Category::getName, categoryPageDto.getName())
                .eq(!Objects.isNull(categoryPageDto.getStatus()), Category::getStatus, categoryPageDto.getStatus());

        // 构造分页条件
        Page<Category> pageQuery = new Page<>(categoryPageDto.getPageNum(), categoryPageDto.getPageSize());

        // 执行查询
        Page<Category> page = page(pageQuery, queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(), page.getTotal()));
    }

    @Override
    public ResponseResult addCategory(CategoryAddDto categoryAddDto) {
        if(!list(new LambdaQueryWrapper<Category>().eq(Category::getName, categoryAddDto.getName()).eq(Category::getDelFlag, SystemConstants.NOT_DELETED)).isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CATEGORY_NAME_EXIST);
        }
        return save(BeanUtil.copyBean(categoryAddDto, Category.class)) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }
}

